#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int pid;
    int arrival_time;
    int burst_time;
    int remaining_time;
    int completion_time;
    int turnaround_time;
    int waiting_time;
} Process;

void calculateTimes(Process *processes, int n, int time_slice) {
    int current_time = 0;
    int completed = 0;

    while (completed < n) {
        for (int i = 0; i < n; i++) {
            if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
                if (processes[i].remaining_time <= time_slice) {
                    current_time += processes[i].remaining_time;
                    processes[i].remaining_time = 0;
                    processes[i].completion_time = current_time;
                    processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
                    processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
                    completed++;
                } else {
                    current_time += time_slice;
                    processes[i].remaining_time -= time_slice;
                }
            }
        }
    }
}

int main() {
    int time_slice = 4;
    int n = 4;

    Process processes[] = {
        {1, 0, 10, 10, 0, 0, 0},
        {2, 1, 6, 6, 0, 0, 0},
        {3, 2, 8, 8, 0, 0, 0},
        {4, 3, 4, 4, 0, 0, 0}
    };

    calculateTimes(processes, n, time_slice);

    for (int i = 0; i < n; i++) {
        printf("P%d:%d,%d,%d\n", 
               processes[i].pid, processes[i].completion_time, 
               processes[i].turnaround_time, processes[i].waiting_time);
    }

    return 0;
}
